/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;

import java.util.logging.Logger;

import l1j.server.server.ClientThread;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_Message_YN;
import l1j.server.server.serverpackets.S_ServerMessage;

// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

public class C_RequestCreateParty extends ClientBasePacket {

	public C_RequestCreateParty(byte decrypt[], ClientThread client)
			throws Exception {
		super(decrypt);
		int c = readC();
		int targetobjectId = readD();
		l1j.server.server.model.Instance.L1PcInstance player = client
				.getActiveChar();

		L1Object temp = L1World.getInstance().findObject(targetobjectId);
		if (temp instanceof L1PcInstance) {
			L1PcInstance target = (L1PcInstance) temp;
			if (target.isInParty()) // 相手がパーティー中
			{
				player.sendPackets(new S_ServerMessage(415)); // すでに他のパーティーに所属しているため招待できません
			} else // 相手がパーティー中でない
			{
				if (player.getId() != target.getId()) // プレイヤーと相手が異なる
				{
					if (player.isInParty()) // プレイヤーがパーティー中
					{
						if (player.getParty().isLeader(player)) // プレイヤーがパーティーリーダー
						{
							target.setPartyID(player.getId());
							target.sendPackets(new S_Message_YN(953, player
									.getName())); // パーティー招待を許可しますか？（Y/N）
						} else {
							player.sendPackets(new S_ServerMessage(416)); // パーティーのリーダーのみが招待できます。
						}
					} else // プレイヤーがパーティー中でない
					{
						target.setPartyID(player.getId());
						target.sendPackets(new S_Message_YN(953, player
								.getName())); // パーティー招待を許可しますか？（Y/N）
					}
				}
			}
		}
	}

	@Override
	public String getType() {
		return "[C] C_RequestCreateParty";
	}

	private static final String _C__0B_REQUESTCREATEPARTY = "[C] C_RequestCreateParty";
	private static Logger _log = Logger.getLogger(C_RequestCreateParty.class
			.getName());

}
